//
//  SettingsViewController.m
//  SchoolIsOver
//
//  Created by liu ming on 11-6-15.
//  Copyright 2011 peixin. All rights reserved.
//

#import "SettingsViewController.h"


@implementation SettingsViewController

@synthesize schoolNameLabel, schoolNameTextField;

@synthesize gradeOneLabel, gradeTwoLabel, gradeThreeLabel, gradeFourLabel, gradeFiveLabel, gradeSixLabel;
@synthesize gradeOneTextFiled, gradeTwoTextFiled, gradeThreeTextFiled, gradeFourTextFiled, gradeFiveTextFiled, gradeSixTextFiled;

@synthesize serviceAddressTextField;

@synthesize aboutLabel;

@synthesize Settings;

#pragma mark -
#pragma mark 响应用户输入数据完成
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	
	return YES;
}

#pragma mark -
#pragma mark Interface element init
-(void)initSchoolSettings
{
	schoolNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
	schoolNameLabel.text = @"学校名称：";
	
	schoolNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 15, 180, 23)];
	schoolNameTextField.placeholder = @"填入学校名称";
	schoolNameTextField.delegate = self;
	schoolNameTextField.returnKeyType = UIReturnKeyDone;
	schoolNameTextField.text = [Settings schoolName];
}

-(void)initGradeSettings
{
	gradeOneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeOneLabel.text = @"一年级班级数量：";
	gradeOneTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeOneTextFiled.placeholder = @"班级数量(0-99)";
	gradeOneTextFiled.delegate = self;
	gradeOneTextFiled.returnKeyType = UIReturnKeyDone;
	gradeOneTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeOneCount]];
	
	gradeTwoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeTwoLabel.text = @"二年级班级数量：";
	gradeTwoTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeTwoTextFiled.placeholder = @"班级数量(0-99)";
	gradeTwoTextFiled.delegate = self;
	gradeTwoTextFiled.returnKeyType = UIReturnKeyDone;
	gradeTwoTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeTwoCount]];
	
	gradeThreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeThreeLabel.text = @"三年级班级数量：";
	gradeThreeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeThreeTextFiled.placeholder = @"班级数量(0-99)";
	gradeThreeTextFiled.delegate = self;
	gradeThreeTextFiled.returnKeyType = UIReturnKeyDone;
	gradeThreeTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeThreeCount]];
	
	gradeFourLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeFourLabel.text = @"四年级班级数量：";
	gradeFourTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeFourTextFiled.placeholder = @"班级数量(0-99)";
	gradeFourTextFiled.delegate = self;
	gradeFourTextFiled.returnKeyType = UIReturnKeyDone;
	gradeFourTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeFourCount]];

	gradeFiveLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeFiveLabel.text = @"五年级班级数量：";
	gradeFiveTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeFiveTextFiled.placeholder = @"班级数量(0-99)";
	gradeFiveTextFiled.delegate = self;
	gradeFiveTextFiled.returnKeyType = UIReturnKeyDone;
	gradeFiveTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeFiveCount]];
	
	gradeSixLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 140, 30)];
	gradeSixLabel.text = @"六年级班级数量：";
	gradeSixTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(170, 13, 120, 23)];
	gradeSixTextFiled.placeholder = @"班级数量(0-99)";
	gradeSixTextFiled.delegate = self;
	gradeSixTextFiled.returnKeyType = UIReturnKeyDone;
	gradeSixTextFiled.text = [[NSString alloc]initWithFormat:@"%@",[Settings gradeSixCount]];
}

-(void)initNetSettings
{
	serviceAddressTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 13, 280, 23)];
	serviceAddressTextField.placeholder = @"网络服务器地址";
	serviceAddressTextField.delegate = self;
	serviceAddressTextField.returnKeyType = UIReturnKeyDone;
	serviceAddressTextField.text = [Settings serviceAddress];
}

-(void)saveSettings:(id) sender
{
	Settings.schoolName = schoolNameTextField.text;
	
	Settings.gradeOneCount = [NSNumber numberWithInt:[gradeOneTextFiled.text intValue]];
	Settings.gradeTwoCount = [NSNumber numberWithInt:[gradeTwoTextFiled.text intValue]];
	Settings.gradeThreeCount = [NSNumber numberWithInt:[gradeThreeTextFiled.text intValue]];
	Settings.gradeFourCount = [NSNumber numberWithInt:[gradeFourTextFiled.text intValue]];
	Settings.gradeFiveCount = [NSNumber numberWithInt:[gradeFiveTextFiled.text intValue]];
	Settings.gradeSixCount = [NSNumber numberWithInt:[gradeSixTextFiled.text intValue]];
	
	Settings.serviceAddress = serviceAddressTextField.text;
	
	[Settings save];
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

	self.navigationItem.title = @"系统设置";
	UIBarButtonItem	* saveButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" 
																	style:UIBarButtonItemStylePlain
																   target:self
																   action:@selector(saveSettings:)];
	self.navigationItem.rightBarButtonItem = saveButton;
	
	Settings = [[SchoolIsOverSettings alloc] init];
	
	[self initSchoolSettings];
	[self initGradeSettings];
	[self initNetSettings];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	NSLog(@"TMD");
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger count = 0;
    switch (section) {
		case 0:
			count = 1;
			break;
		case 1:
			count = 6;
			break;
		case 2:
			count = 1;
			break;
		case 3:
			count = 1;
			break;
		default:
			break;
	}
	
	return count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
    switch ([indexPath section]) {
		case 0:
			if ([indexPath row] == 0) {
				[cell addSubview:schoolNameLabel];
				[cell addSubview:schoolNameTextField];
			}
			break;
		case 1:
			switch ([indexPath row]) {
				case 0:
					[cell addSubview:gradeOneLabel];
					[cell addSubview:gradeOneTextFiled];
					break;
				case 1:
					[cell addSubview:gradeTwoLabel];
					[cell addSubview:gradeTwoTextFiled];
					break;
				case 2:
					[cell addSubview:gradeThreeLabel];
					[cell addSubview:gradeThreeTextFiled];
					break;
				case 3:
					[cell addSubview:gradeFourLabel];
					[cell addSubview:gradeFourTextFiled];
					break;
				case 4:
					[cell addSubview:gradeFiveLabel];
					[cell addSubview:gradeFiveTextFiled];
					break;
				case 5:
					[cell addSubview:gradeSixLabel];
					[cell addSubview:gradeSixTextFiled];
					break;
				default:
					break;
			}
			
			break;
		case 2:
			[cell addSubview:serviceAddressTextField];
			break;
		default:
			break;
	}
    
    return cell;
}

-(NSString *) tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section
{
	NSString * strTitle = [[NSString alloc] initWithFormat:@"",""];
	switch (section) {
		case 0:
			strTitle = [[NSString alloc] initWithFormat:@"%@", @"学校信息"];
			break;
		case 1:
			strTitle = [[NSString alloc] initWithFormat:@"%@", @"班级设置"];
			break;
		case 2:
			strTitle = [[NSString alloc] initWithFormat:@"%@", @"网络设置"];
			break;
		case 3:
			strTitle = [[NSString alloc] initWithFormat:@"%@", @"关于"];
			break;
		default:
			break;
	}
	
	return strTitle;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/




#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    schoolNameLabel = nil;	
	schoolNameTextField = nil;
	
	gradeOneLabel = nil;
	gradeTwoLabel = nil;
	gradeThreeLabel = nil;
	gradeFourLabel = nil;
	gradeFiveLabel = nil;
	gradeSixLabel = nil;
	
	gradeOneTextFiled = nil;
	gradeTwoTextFiled = nil;
	gradeThreeTextFiled =nil;
	gradeFourTextFiled = nil;
	gradeFiveTextFiled = nil;
	gradeSixTextFiled = nil;
	
	serviceAddressTextField = nil;
	
	aboutLabel = nil;
	
	Settings = nil;
}


- (void)dealloc {
	[schoolNameLabel release];
	[schoolNameTextField release];
	
	[gradeOneLabel release];
	[gradeTwoLabel release];
	[gradeThreeLabel release];
	[gradeFourLabel release];
	[gradeFiveLabel release];
	[gradeSixLabel release];
	
	[gradeOneTextFiled release];
	[gradeTwoTextFiled release];
	[gradeThreeTextFiled release];
	[gradeFourTextFiled release];
	[gradeFiveTextFiled release];
	[gradeSixTextFiled release];
	
	[serviceAddressTextField release];	
	
	[aboutLabel release];
	
	[Settings release];
	
    [super dealloc];
}


@end

